<?php

require_once(DIR_CLASSES_DB_CORE . "db_config_wrapper.class.php");
require_once(DIR_CLASSES_SMARTY . "smarty_config_wrapper.class.php");

class global_config {
	private $dbconfig;
	private $paginate_limit;
	
	public $paginate;
	public $root_dir;
	public $root_url;
	public $_url;
	public $allow_remote_scripting;
	public $_SERVER;
	
	public $classmap=array();
	
	public $smarty_config;
	public $ajax_debug = false;
	
	public $revision = 1;
	
	public $links_view = "default";
	
	private static $_instance;

	/**
	 * Returns the only instance of the class
	 *
	 * @example $gconfig = global_config::get_instance();
	 * @return global_config
	 */
	public static function &get_instance() {
		if (!isset(self::$_instance)) {
			$c = __CLASS__;
			self::$_instance = new $c;
		}
		
		return self::$_instance;
	}

	/**
	 * Constructor
	 *
	 * @return global_config
	 */
	private function __construct() {
		ignore_user_abort(true);
		mb_internal_encoding("UTF-8");

		$this->root_dir = realpath(dirname(__FILE__)."/../../../");
		$this->_url = $_SERVER['SERVER_NAME'];
		$this->dbconfig = new db_config_wrapper();
		$this->dbconfig->set_host_name("localhost");
		$this->dbconfig->set_user_name("root");
		$this->dbconfig->set_password("123");
		$this->dbconfig->set_name("test");
		$this->dbconfig->set_type("mysql");
		$this->dbconfig->set_debug(false);
		
		$this->allow_remote_scripting = true;

		$this->root_url = 'http://Complex/';
		
		$this->_SERVER = 'http://192.168.0.1/Services/Service.asmx';
		
		$tpl_dir = $this->root_dir . "/" . DIR_TEMPLATES;
		$this->smarty_config = new smarty_config_wrapper();
		$this->smarty_config->set_smarty_debug(false);
		$this->smarty_config->set_tpl_compile_check(true);
		$this->smarty_config->set_tpl_path($tpl_dir . 'templates');
		$this->smarty_config->set_tpl_compiled_path($tpl_dir . 'templates_c');
		$this->smarty_config->set_tpl_config_path($tpl_dir . 'config');
		$this->smarty_config->set_tpl_cache_path($tpl_dir . 'cache');
		
		// smarty_paginate parameters
		$this->paginate['first_text'] = '[&lt;&lt;]'; // [<<]
		$this->paginate['last_text'] = '[&gt;&gt;]'; // [>>]
		$this->paginate['next_text'] = '[&gt;]';
		$this->paginate['prev_text'] = '[&lt;]';
		$this->paginate['middle_page_limit'] = 10;
		$this->paginate['middle_format'] = "page"; // "page" = [1][2][3], "" = [1-10][11-20]
		$this->paginate['middle_prefix'] = "";
		$this->paginate['middle_suffix'] = "";
		$this->paginate['middle_link_prefix'] = " ";
		$this->paginate['middle_link_suffix'] = " ";
		
		$this->paginate_limit['default'] = 20;
	}
	
	/**
	 * Gets db config
	 *
	 * @return db_config_wrapper
	 */
	public function &get_db_config() {
		return $this->dbconfig;
	}
	
	/**
	 * Gets smarty config
	 *
	 * @return smarty_config_wrapper
	 */
	public function &get_smarty_config() {
		return $this->smarty_config;
	}
	/**
	 * Gets server adress
	 *
	 * @return ip adres of the server
	 */
	public function &get_server_address() {
		return $this->_SERVER;
	}

	/**
	 * Add to classmap class
	 *
	 * 
	 */
	public function add_server_class($c, $php) {
		 $this->classmap = /*array_unique (*/array_merge($this->classmap, array($c=> $php))/*)*/;
	}
	
	/**
	 * Gets paginate limit
	 *
	 * @param string $module
	 * @param string $action
	 * @return int
	 */
	public function get_paginate_limit($module = "", $action = "") {
		return isset($this->paginate_limit[$module][$action]) ? $this->paginate_limit[$module][$action] : $this->paginate_limit['default'];
	}
}

$gconfig = global_config::get_instance();
?>